package com.quanshu.shop.person.view;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.shop.base.tool.BaseTool;
import com.quanshu.shop.base.view.BaseTitleBar;
import com.quanshu.shop.goods.view.GoodsInfoActivity;
import com.quanshu.shop.main.api.ShopMainIF;
import com.quanshu.shop.person.adapter.LikeGoodsAdapter;
import com.quanshu.shop.person.entity.MylikeEntity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyLikeActivity extends BaseActivity {

    @BindView(R.id.my_like_title)
    BaseTitleBar myLikeTitle;
    @BindView(R.id.my_like_rl)
    RecyclerView myLikeRl;
    private LikeGoodsAdapter likeGoodsAdapter;
    private List<MylikeEntity.DataEntity> dataEntityList;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_like);
        ButterKnife.bind(this);
        mContext = this;
        ImmersionBar.with(this).statusBarColor(R.color.white).statusBarDarkFont(true).titleBar(myLikeTitle).init();
        initView();
    }

    private void initView() {
        myLikeTitle.setLeftImage(R.drawable.icon_back_black_2_0).setCenterText("我的收藏").setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {

            }
        });
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        myLikeRl.setLayoutManager(gridLayoutManager);
        NetClient.getInstance().getShopService(ShopMainIF.class).getLikeGoodsInfo(AppUtils.getSignParams(currentUser.getId()), currentUser.getId())
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse<List<MylikeEntity.DataEntity>>>() {
                    @Override
                    public void onSuccess(BaseResponse<List<MylikeEntity.DataEntity>> response) {
                        dataEntityList = response.getData();
                        likeGoodsAdapter = new LikeGoodsAdapter(dataEntityList, mContext);
                        likeGoodsAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                            @Override
                            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                switch (view.getId()) {
                                    case R.id.like_goods:
                                        Bundle bundle = new Bundle();
                                        bundle.putString("goods_info_id", dataEntityList.get(position).good_id);
                                        BaseTool.goActivity(mContext, GoodsInfoActivity.class, bundle);
                                        break;
                                }
                            }
                        });
                        myLikeRl.setAdapter(likeGoodsAdapter);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
